declare @q table (id int, pid int, text nvarchar(max), surveyId int);

with s(id, parentId, text, surveyId) as
(
select f.id, f.parentId, cast(f.text as nvarchar(max)) as text, surveyId from Questions f 
where f.parentId is null and f.surveyId = 2
union all
select f.id, f.parentId, cast(s.text + ' ' + f.text as nvarchar(max)) as text, f.surveyId from Questions f inner join s on f.parentId = s.id
where f.surveyId = 2
)
insert into @q
select * from s
where not exists(select * from Questions q where q.parentId  = s.id)

select b.login, b.text as question, a.value, case when a.id is null then 0 else 1 end answered  from 
(select ss.id as sesId, u.id as usId , u.login, s.name, q.text,q.id as questID from SurveySessions ss
inner join Users u on u.id=ss.userId
inner join Surveys s on s.id=ss.surveyId
inner join @q q on q.surveyId = s.id) b
left outer join Answers a on a.questionId=b.questID and a.sessionId=b.sesId